<?php 
class askplatdb2couchdbController extends ZendX_Controller_Action
{	
	public function asklistAction()
	{		
		$asks = $this->db('askplat')->fetchAll("select  top 1000 't_askplat_asklist' as doctype, a.*, at.asklist_type_name, ast.status_name,att.ask_type_name,ar.asklist_right_name, area.area_name, u.nick_name
					from dbo.t_askplat_asklist a left join
					dbo.t_askplat_asklist_type at on a.asklist_type = at.asklist_type_id
					left join dbo.t_askplat_asklist_status ast on a.asklist_status = ast.asklist_status_id
					left join dbo.t_askplat_ask_type att on a.ask_type = att.ask_type_id
					left join dbo.t_askplat_asklist_right ar on a.asklist_right = ar.asklist_right_id
					left join dbo.t_askplat_area area on a.ask_area = area.area_id
					left join dbo.t_askplat_askgroup_user u on a.deal_u_id = u.u_id");
		
		
		//asklist_items
		foreach ($asks as $k=>$a){
			$items = $this->db('askplat')->fetchAll("SELECT *  FROM t_askplat_item where ask_id = {$a['ask_id']}");
			$asks[$k]['item'] = $items;
		}
		
		//project
		foreach ($asks as $k=>$a){
			$project = $this->db('askplat')->fetchAll("SELECT p.*, pp.process_name
				  FROM t_askplat_asklist_project p,
				  dbo.t_askplat_asklist_project_process pp
				  where p.project_process=pp.process_id and p.ask_id = {$a['ask_id']}");
			$asks[$k]['project'] = $project;
		}
		
		//comment
		foreach ($asks as $k=>$a){
			$comment = $this->db('askplat')->fetchAll("select * FROM t_askplat_asklist_comment where ask_id = {$a['ask_id']}");
			$asks[$k]['comment'] = $comment;
		}
		
		//comment_reply
		foreach ($asks as $k=>$a){
			$comment_reply = $this->db('askplat')->fetchAll("select * FROM t_askplat_asklist_comment_reply where ask_id = {$a['ask_id']}");
			$asks[$k]['comment_reply'] = $comment_reply;
		}
		
		//attachments
		
		//reply
		foreach ($asks as $k=>$a){
			$reply = $this->db('askplat')->fetchAll("select * FROM t_askplat_reply where ask_id = {$a['ask_id']}");
			//reply_item
			foreach ($reply as $i=>$r)
			{
				$items = $this->db('askplat')->fetchAll("select ri.* from dbo.t_askplat_reply_item ri where ri.reply_id =?", $r['reply_id']);
				$reply[$i]['reply_items'] = $items;	
			}
			
			$asks[$k]['reply'] = $reply;
		}		
		
		$this->couchClient()->storeDocs($asks);	
		
		return $this->_helper->json(array('sucess'=>true));
	}
	
	public function asklistattachmentsAction()
	{
		$at = $this->db('askplat')->fetchAll("SELECT t.*  FROM dbo.t_askplat_asklist_attachment t, dbo.t_askplat_asklist a  where t.ask_id = a.ask_id");
		
		
		foreach ($at as $a){
			$d = $this->couchClient()->key($a['ask_id'])->getView('services', 'getAsklistById');			
			$doc = $this->couchClient()->getDoc($d->rows[0]->id);
			$fd = file_get_contents("http://askplat.m2.com.cn:8080/v2/data/{$a['file_path']}");
			$fn = substr($a['file_path'], 0, strlen($a['file_path'])-3);
			
			$this->couchClient()->storeAsAttachment($doc, $fd, $fn);
		}	

		return $this->_helper->json(array('sucess'=>true));
	}
	
	public function supplierAction()
	{
		$suppliers = $this->db('askplat')->fetchAll("select 't_askplat_supplier' as doctype, s.*, sf.*
			from dbo.t_askplat_supplier s
			left join dbo.t_askplat_supplier_field sf
			on s.supplier_id = sf.supplier_id");
		
		//contact
		foreach ($suppliers as $k=>$s)
		{
			$contacts = $this->db('askplat')->fetchAll("select * from dbo.t_askplat_supplier_contact where supplier_id = ?", $s['supplier_id']);
			$suppliers[$k]['contacts'] = $contacts;
		}
		
		//keywords
		foreach ($suppliers as $k=>$s)
		{
			$keywords = $this->db('askplat')->fetchAll("select sk.*, k.*
					from dbo.t_askplat_supplier_keyword sk, dbo.t_askplat_keyword k
					where sk.keyword_id = k.keyword_id
					and sk.supplier_id = ?", $s['supplier_id']);
			
			$suppliers[$k]['keywords'] = $keywords;
		}
		
		$this->couchClient()->storeDocs($suppliers);
		
		exit();
	}
	
	public function noticeAction()
	{
		$notices = $this->db('askplat')->fetchAll("select 't_askplat_notice' as doctype, * from dbo.t_askplat_notice");
		
		$this->couchClient()->storeDocs($notices);
		exit();
	}
	
	public function messageAction()
	{
		$items = $this->db('askplat')->fetchAll("select 't_askplat_message' as doctype, * from dbo.t_askplat_message");
		
		$this->couchClient()->storeDocs($items);
		exit();	
	}
	
	public function userAction()
	{
		$users = $this->db('askplat')->fetchAll("select 't_askplat_askgroup_user' as doctype, u.*, up.*, qs.*
			from dbo.t_askplat_askgroup_user u
			left join dbo.t_askplat_askgroup_position up on u.position_id = up.position_id
			left join dbo.t_askplat_askgroup_qq_status qs on u.qq_status = qs.qq_status_id");
		
		foreach ($users as $k=>$u)
		{
			$privileges = $this->db('askplat')->fetchAll("select ur.*, r.*
				from dbo.t_askplat_askgroup_right ur, dbo.t_askplat_askgroup_schedule r
				where ur.schedule_id = r.schedule_id
				and ur.u_id =?", $u['u_id']);
			
			$users[$k]['privileges'] = $privileges;
		}
		
		$this->couchClient()->storeDocs($users);
		exit();		
	}
}